package com.example.controller;

import com.aiseed.entity.User;
import com.aiseed.mapper.UserMapper;
import com.aiseed.service.impl.UserServiceImpl;
import com.aiseed.utils.Result;
import com.aiseed.utils.ResultUtils;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
//@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/get")
    public User getUser(){
        User user=new User("1001","testman","12345","10016");
        return user;
    }
    @RequestMapping("/list")
    public List<User> getUsers(){
        System.out.println(("---- selectAll method test ------"));
        List<User> list=userMapper.selectList(null);
        return list;
    }
    @RequestMapping("/login")
    public Result login(User user){
        boolean isLogin=userService.login(user.getUsername(),user.getPassword());
        Result result= ResultUtils.success(isLogin);
        return result;
    }
/*    @GetMapping("login")
    public String login(String username,String password){
        if(StringUtils.isEmpty(username)){
            return "用户名不允许为空";
        }
        if(StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }
        User user=userMapper.selectUser(username);
        if (user == null){
            return "登录失败";
        }
        if(Objects.equals(password,user.getPassword())){
            return "登录成功";
        }
        return "登录失败,密码错误";
    }*/

    @GetMapping("register")
    public String register(String username,String password){
        log.info("username:{}",username);
        log.info("password:{}",password);
        if(StringUtils.isEmpty(username)){
            return "用户名不允许为空";
        }

        if(StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }

        int resultCount=userMapper.saveUser(username,password);
        if(resultCount == 0){
            return "注册失败";
        }
        return "注册成功";

    }

}